﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GRWL.BZSC.UI.BaseDataSvc;

namespace GRWL.BZSC.UI
{
    public partial class EditWorkstageFrm : BaseFrm
    {

        /// <summary>
        /// 本窗体UpdateTreeviewDelegate发送
        /// </summary>
        public event GRWL.BZSC.Common.EventCommon.UpdateTreeViewDlgt UpdateTreeviewEvent;
        public int Id { get; set; }
        public int ParentId { get; set; }
        public bool EditMode { get; set; }

        /// <summary>
        /// 临时变量
        /// </summary>
        private TB_Workstage Workstage { get; set; }

        /// <summary>
        /// 编码规则
        /// </summary>
        //public CodeRuleFrm.CodeRule codeRule { get; set; }

        public EditWorkstageFrm()
        {
            InitializeComponent();
            this.BaseDataProxy = new BaseDataClient();
            this.BaseDataProxy.Open();
        }

        private void EditWorkstageFrm_Load(object sender, EventArgs e)
        {
            if (this.EditMode)
            {
                this.txtBoxCode.Enabled = false;
                Workstage = this.BaseDataProxy.GetWorkstage(this.Id);
                if (Workstage != null)
                {
                    this.Text = "编辑" + this.Text;
                    this.txtBoxCode.Text = Workstage.code;
                    this.txtBoxHourFee.Text = Workstage.hourFee.ToString();
                    this.txtBoxHourWage.Text = Workstage.hourWage.ToString();
                    this.txtBoxName.Text = Workstage.name;
                    this.txtBoxPreManhour.Text = Workstage.preManhour.ToString();
                    this.txtBoxPrice.Text = Workstage.price.ToString();
                    this.txtBoxStdManhour.Text = Workstage.stdManhour.ToString();
                    this.txtBoxUnitWage.Text = Workstage.unitWage.ToString();
                    this.cbBoxManhourUnit.SelectedItem = Workstage.manhourUnit;
                    this.cbBoxMethod.SelectedItem = Workstage.method;
                    this.cbBoxSection.SelectedItem = Workstage.section;
                    this.cbBoxType.SelectedItem = Workstage.type;
                    this.cbBoxUnit.SelectedItem = Workstage.unit;
                    this.ckBoxIsDefault.Checked = Workstage.isDefault;
                    this.ckBoxIsVerify.Checked = Workstage.isVerify;
                }
            }
            else
            {
                if (this.codeRule.autoCode)
                {
                    this.txtBoxCode.Text = this.BaseDataProxy.GetSerialNo((int)GRWL.BZSC.Common.EnumCommon.CodeRuleType.Other, this.codeRule.header, this.codeRule.len,!string.IsNullOrEmpty(this.codeRule.header), this.codeRule.autoFill,false);
                    this.txtBoxCode.Enabled = false;
                }
                this.Text = "新增" + this.Text;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!this.EditMode && !this.codeRule.autoCode && this.txtBoxCode.Text.Trim() == string.Empty)
            {
                MessageBox.Show("请输入代号！");
                this.txtBoxCode.Focus();
                return;
            }

            if (this.txtBoxName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("请输入名称！");
                this.txtBoxName.Focus();
                return;
            }

            Workstage = new TB_Workstage();
            Workstage.code = this.txtBoxCode.Text.Trim();
            if (!string.IsNullOrEmpty(this.txtBoxHourFee.Text))
                Workstage.hourFee = Convert.ToDecimal(this.txtBoxHourFee.Text);
            if (!string.IsNullOrEmpty(this.txtBoxHourWage.Text))
                Workstage.hourWage = Convert.ToDecimal(this.txtBoxHourWage.Text);
            Workstage.isDefault = this.ckBoxIsDefault.Checked;
            Workstage.isVerify = this.ckBoxIsVerify.Checked;
            Workstage.manhourUnit = this.cbBoxManhourUnit.Text;
            Workstage.method = this.cbBoxMethod.Text;
            Workstage.name = this.txtBoxName.Text.Trim();
            Workstage.parentId = this.ParentId;
            if (!string.IsNullOrEmpty(this.txtBoxPreManhour.Text))
                Workstage.preManhour = Convert.ToDecimal(this.txtBoxHourWage.Text);
            if (!string.IsNullOrEmpty(this.txtBoxPrice.Text))
                Workstage.price = Convert.ToDecimal(this.txtBoxPrice.Text);
            Workstage.section = this.cbBoxSection.Text;
            if (!string.IsNullOrEmpty(this.txtBoxStdManhour.Text))
                Workstage.stdManhour = Convert.ToDecimal(this.txtBoxStdManhour.Text);
            Workstage.type = this.cbBoxType.Text;
            Workstage.unit = this.cbBoxUnit.Text;
            if (!string.IsNullOrEmpty(this.txtBoxUnitWage.Text))
                Workstage.unitWage = Convert.ToDecimal(this.txtBoxUnitWage.Text);
            Workstage.wageCalculation = this.cbBoxWageCalculation.Text;

            Common.OperateType opType = Common.OperateType.Add;
            bool ok = false;
            if (this.EditMode)
            {
                Workstage.id = this.Id;
                ok = this.BaseDataProxy.UpdateWorkstage(Workstage);
                opType = Common.OperateType.Update;
            }
            else
            {
                int newId = this.BaseDataProxy.AddWorkstage(Workstage);
                if (newId > 0)
                {
                    Workstage.id = newId;
                    if (this.codeRule != null && this.codeRule.autoCode)
                        Workstage.code = this.BaseDataProxy.GetSerialNo((int)GRWL.BZSC.Common.EnumCommon.CodeRuleType.Other, this.codeRule.header, this.codeRule.len,!string.IsNullOrEmpty(this.codeRule.header), this.codeRule.autoFill,false);
                    else
                        Workstage.code = this.txtBoxCode.Text.Trim();
                    ok = this.BaseDataProxy.UpdateWorkstage(Workstage);
                }
            }

            if (ok)
            {
                CommonEventArg<TB_Workstage> arg = new CommonEventArg<TB_Workstage>(Workstage, opType);
                GRWL.BZSC.Common.EventCommon.OnWorkstageUdTVEvnt(sender, arg);
            }

            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
